<template>
  <div>
    <!-- 查找 -->
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="">
        <el-input v-model="formInline.user" placeholder="name"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button type="primary" @click="dialogVisible = true">增加</el-button>
      </el-form-item>
    </el-form>
    <!-- 表格 -->
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column fixed prop="date" label="日期"> </el-table-column>
      <el-table-column prop="name" label="姓名"> </el-table-column>
      <el-table-column prop="address" label="地址"> </el-table-column>
      <el-table-column prop="zip" label="邮编"> </el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 增加 -->
    <el-dialog
      title="增加"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <span>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="用户名">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="地址">
            <el-input v-model="form.region"></el-input>
          </el-form-item>
          <el-form-item label="创建日期">
            <el-input v-model="form.date1"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmitadd">立即增加</el-button>
            <el-button @click="dialogVisible = false">取消</el-button>
          </el-form-item>
        </el-form>
      </span>
    </el-dialog>
    <!-- 修改 -->
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [
        {
          date: "2016-05-02",
          name: "王虎",
          address: "上海市普陀区金沙江路 1518 弄",
          zip: 200333,
        },
        {
          date: "2016-05-04",
          name: "王大虎",
          address: "上海市松江区金沙江路 1517 弄",
          zip: 200333,
        },
        {
          date: "2016-05-01",
          name: "王中虎",
          address: "上海市高新区金沙江路 1519 弄",
          zip: 200333,
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市虹桥区金沙江路 1516 弄",
          zip: 200333,
        },
      ],
      // 查询
      formInline: {
        user: "",
      },
      // 增加
      dialogVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
      },
      // 修改
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
      // 删除弹框
      this.$confirm("此操作将永久删除该用户信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData.splice(index, 1),
            this.$message({
              type: "success",
              message: "删除成功!",
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 查询
    onSubmit() {
      for (let i = 0; i < this.tableData.length; i++) {
        if (this.formInline.user === this.tableData[i].name) {
          alert(this.tableData[i].address);
        }
      }
    },
    // 增加
    handleClose(done) {
      this.$confirm("确认增加？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    onSubmitadd() {
      this.tableData.push(
        {
          date: this.form.date1,
          name: this.form.name,
          address: this.form.region,
          zip: '000000',
        }
      ),
      this.dialogVisible = false,
      // 清空输入框
      this.form.date1='',
      this.form.name='',
      this.form.region=''

    },
    // 修改
  },
};
</script>

<style lang="scss" scoped></style>
